//
//  VideoPlayerViewController.swift
//  ZNZXSuperMarket
//
//  Created by 四川中农 on 2018/9/11.
//  Copyright © 2018年 四川中农. All rights reserved.
//

import UIKit
import BMPlayer
import Alamofire

class VideoPlayerViewController: BaseViewController {
    var url = ""
    var name = ""
    var player:BMPlayer!
    let manager = NetworkReachabilityManager(host: "https://www.baidu.com/")
    
    lazy var alertView : AlertContentView = {
        let view = AlertContentView.init(frame: CGRect.init(x: 0, y: 0, width: UIScreen.ScreenWidth, height: UIScreen.ScreenHeight))
        view.backClosure = { [weak self] in
            self?.setVideo()
        }
        view.leftClosure = { [weak self] in
            self?.dismiss(animated: false, completion: nil)
        }
        UIApplication.shared.keyWindow?.addSubview(view)
        return view
    }()
    
    override func viewDidLoad() {
        super.viewDidLoad()
        
        player = BMPlayer.init()
        player.frame = self.view.bounds
        self.view.addSubview(player)
        player.backBlock = { [unowned self] (isFullScreen) in
            if isFullScreen == true { return }
            self.dismiss(animated: false, completion: nil)
        }
        
        if manager?.isReachable ?? false{
            if manager?.isReachableOnWWAN ?? false {
                self.alertView.show(withTitle: "当前正在使用移动网络,会消耗流量,是否播放？")
            } else{
                self.setVideo()
            }
        } else {
            PKhud.showText(text: "网络不可用~")
        }
        
    }
    
    func setVideo(){
        let asset = BMPlayerResource(url: URL(string: self.url)!,
                                     name: name)
        player.setVideo(resource: asset)
    }

    override func didReceiveMemoryWarning() {
        super.didReceiveMemoryWarning()
        // Dispose of any resources that can be recreated.
    }
    

    /*
    // MARK: - Navigation

    // In a storyboard-based application, you will often want to do a little preparation before navigation
    override func prepare(for segue: UIStoryboardSegue, sender: Any?) {
        // Get the new view controller using segue.destinationViewController.
        // Pass the selected object to the new view controller.
    }
    */

}
